package data

import groovy.sql.Sql

//可以指定maven仓库
//@GrabResolver(name = 'aliyun', root = 'http://maven.aliyun.com/nexus/content/groups/public/')
//加载数据库连接驱动包
@Grab('mysql:mysql-connector-java:5.1.25')
@GrabConfig(systemClassLoader = true)
//创建sql实例
def getSql(){
    url='jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8'
    driver='com.mysql.jdbc.Driver'
    username='root'
    passwd='799827577'
    return Sql.newInstance(url, username, passwd, driver)
}
def sql = getSql()
println sql.connection.catalog
sql.eachRow("select * from `user`") {
    println it[0]  //可以按表字段从左到右依次下标访问
    println it.name  //直接按字段名访问
}
//支持元数据闭包显示
sql.eachRow("select * from `user`",{ meta->
    //元数据闭包只会调用一次
    meta.columnCount.times {
        print meta.getColumnName(it+1)+"  "
        print meta.getColumnClassName(it+1)+"  "
        print meta.getColumnDisplaySize(it+1)+"  "
        print meta.getColumnType(it+1)+"---"
        println meta.getColumnTypeName(it+1)
    }
}) {
    //可以按表字段从左到右依次下标访问或者字段名字访问
    println it[0]+"  "+it.name
}
//查询所有,支持非常多的参数重载方法
println sql.rows(['id':3],"select * from `user` where id>:id").size()  //map参数
println sql.rows("select * from `user` where id>?",[3]).size() // list参数
println sql.firstRow("select * from `user`") //返回第一个

